package com.ruoyi.web.users;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.document.domain.DocBaseBank;
import com.ruoyi.document.domain.DocHighBank;
import com.ruoyi.document.domain.DocInformationsBank;
import com.ruoyi.document.service.IDocBaseBankService;
import com.ruoyi.document.service.IDocHighBankService;
import com.ruoyi.document.service.IDocInformationsBankService;
import com.ruoyi.users.domain.*;
import com.ruoyi.users.vo.DownloadVo;
import com.ruoyi.users.vo.HistoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.users.service.IUDownloadService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 下载记录Controller
 *
 * @author Amy
 * @date 2024-12-03
 */
@RestController
@RequestMapping("/users/download")
public class UDownloadController extends BaseController
{
    @Autowired
    private IUDownloadService uDownloadService;
    @Autowired
    private IDocBaseBankService iDocBaseBankService;
    @Autowired
    private IDocHighBankService iDocHighBankService;
    @Autowired
    private IDocInformationsBankService iDocInformationsBankService;

    /**
     * 查询下载记录列表
     */
    @PreAuthorize("@ss.hasPermi('users:download:list')")
    @GetMapping("/list")
    public TableDataInfo list(UDownload uDownload)
    {
        startPage();
        List<UDownload> list = uDownloadService.selectUDownloadList(uDownload);
        ArrayList<DownloadVo> downloadVos = new ArrayList<>();
        for (UDownload download : list) {
            DownloadVo downloadVo = new DownloadVo();
            Long aLong = download.getbId();
            Long aLong1 = download.gethId();
            Long aLong2 = download.getiId();
            if(aLong!=null){
                DocBaseBank docBaseBank = iDocBaseBankService.selectDocBaseBankById(aLong);
                String name = docBaseBank.getName();
                downloadVo.setTitle(name);
                downloadVo.setCreateTime(download.getCreateTime());
                downloadVo.setCreateBy(download.getCreateBy());
                downloadVo.setRemark(download.getRemark());
                downloadVo.setId(download.getId());
                downloadVos.add(downloadVo);
            }
            if(aLong1!=null){
                DocHighBank docHighBank = iDocHighBankService.selectDocHighBankById(aLong1);
                String name = docHighBank.getName();
                downloadVo.setTitle(name);
                downloadVo.setCreateTime(download.getCreateTime());
                downloadVo.setCreateBy(download.getCreateBy());
                downloadVo.setRemark(download.getRemark());
                downloadVo.setId(download.getId());
                downloadVos.add(downloadVo);
            }
            if(aLong2!=null){
                DocInformationsBank docInformationsBank = iDocInformationsBankService.selectDocInformationsBankById(aLong2);
                String name = docInformationsBank.getName();
                downloadVo.setTitle(name);
                downloadVo.setCreateTime(download.getCreateTime());
                downloadVo.setCreateBy(download.getCreateBy());
                downloadVo.setRemark(download.getRemark());
                downloadVo.setId(download.getId());
                downloadVos.add(downloadVo);
            }


        }
        return getDataTable(downloadVos);
    }

    /**
     * 导出下载记录列表
     */
    @PreAuthorize("@ss.hasPermi('users:download:export')")
    @Log(title = "下载记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UDownload uDownload)
    {
        List<UDownload> list = uDownloadService.selectUDownloadList(uDownload);
        ExcelUtil<UDownload> util = new ExcelUtil<UDownload>(UDownload.class);
        util.exportExcel(response, list, "下载记录数据");
    }

    /**
     * 获取下载记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('users:download:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(uDownloadService.selectUDownloadById(id));
    }

    /**
     * 新增下载记录
     */
    @PreAuthorize("@ss.hasPermi('users:download:add')")
    @Log(title = "下载记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UDownload uDownload)
    {
        return toAjax(uDownloadService.insertUDownload(uDownload));
    }

    /**
     * 修改下载记录
     */
    @PreAuthorize("@ss.hasPermi('users:download:edit')")
    @Log(title = "下载记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UDownload uDownload)
    {
        return toAjax(uDownloadService.updateUDownload(uDownload));
    }

    /**
     * 删除下载记录
     */
    @PreAuthorize("@ss.hasPermi('users:download:remove')")
    @Log(title = "下载记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uDownloadService.deleteUDownloadByIds(ids));
    }
}
